#include "stringUtility.h"
#include <e32base.h>

namespace Util
{
CStringUtility* CStringUtility::NewL(const TDesC& aDes)
{
	CStringUtility* self=CStringUtility::NewLC(aDes);
	CleanupStack::Pop(); // self;
	return self;
}

CStringUtility* CStringUtility::NewLC(const TDesC& aDes)
{
	CStringUtility* self = new CStringUtility;
	CleanupStack::PushL(self);
	if(self)
		self->ConstructL(aDes);
	
	return self;
}

void CStringUtility::ConstructL(const TDesC& aDes)
{
	iBuf = HBufC::NewL(aDes.Length());
	TPtr ptr = iBuf->Des();
	ptr.Copy(aDes);

}

CStringUtility::CStringUtility()
{
	iCurPos = 0;
}

CStringUtility::~CStringUtility()
{
	if(iBuf)
	{
		delete iBuf;
		iBuf = NULL;
	}
}

TPtrC CStringUtility::NextToken(const TDesC& aSep)
{
	if(!iBuf)
		return TPtrC();

	TPtrC rightDes = iBuf->Mid(iCurPos,iBuf->Length()-iCurPos);
	TInt pos = rightDes.Find(aSep);
	if(pos != KErrNotFound)
	{
		TPtrC retPtr(rightDes.Ptr(),pos);
		iCurPos += pos+aSep.Length();

		return retPtr;
	}
	else
	{
		iCurPos = iBuf->Length();
		return TPtrC(rightDes);
	}

}

TBool CStringUtility::Eof()
{
	if(iCurPos >= iBuf->Length())
		return ETrue;
	else
		return EFalse;
}

void CStringUtility::AssignL(const TDesC& aDes)
{
	if(iBuf)
	{
		delete iBuf;
		iBuf = NULL;
	}

	iCurPos = 0;
	ConstructL(aDes);
}

}